package com.icehand.payment.pay.wechat.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;
import com.icehand.common.enums.SignTypeEnum;
import com.icehand.payment.pay.SignGenerator;
import com.icehand.payment.pay.wechat.WeChatPaymentSignatureUtils;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Map;

@Data
@Accessors(chain = true)
public class WeChatAppPayment implements SignGenerator {
	@JSONField(name = "appid")
	private String appId;

	@JSONField(name = "partnerid")
	private String partnerId;

	@JSONField(name = "prepayid")
	private String prepayId;

	@JSONField(name = "timestamp")
	private String timestamp;

	@JSONField(name = "noncestr")
	private String nonceStr;

	@JSONField(name = "package")
	private String packageStr = "Sign=WXPay";

	@JSONField(name = "sign")
	private String sign;

	@Override
	public void createSign(String secret) {
		Map<String,String> map = JSONObject.parseObject(JSON.toJSONString(this),new TypeReference<Map<String, String>>(){});
		this.sign = WeChatPaymentSignatureUtils.generateSignature(map, secret, SignTypeEnum.MD5);
	}
}
